package com.tuanquan.flutter_plugin_tengxunvideo.player;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.tuanquan.flutter_plugin_tengxunvideo.player.model.Constants;
import com.tuanquan.flutter_plugin_tengxunvideo.player.model.LivePlayer;
import com.tuanquan.flutter_plugin_tengxunvideo.player.model.LivePlayerImpl;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class FlutterTXPlayerView implements PlatformView, LivePlayer.OnLivePlayerCallback, MethodChannel.MethodCallHandler {


    private final TXCloudVideoView txCloudVideoView;
    private final LivePlayerImpl mLivePlayer;

    private static final String BIND_ID_TO_NAME = "plugins.player/FlutterTXPlayerView";
    private final MethodChannel methodChannel;

    /**
     * @param context
     * @param messenger
     * @param id
     * @param params    TYPE 播放类型是 标准的 1 还是 低延时直播播放 2   intent_url 直播的URL
     * @param activity
     */
    public FlutterTXPlayerView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params, Activity activity) {
        //初始化
        txCloudVideoView = new TXCloudVideoView(activity);

        methodChannel = new MethodChannel(messenger, BIND_ID_TO_NAME + id);
        methodChannel.setMethodCallHandler(this);


        TXLivePlayer livePlayer = new TXLivePlayer(activity);
        livePlayer.setPlayerView(txCloudVideoView);
        livePlayer.startPlay(params.get("url").toString(),
                TXLivePlayer.PLAY_TYPE_LIVE_FLV);

        mLivePlayer = new LivePlayerImpl(activity, txCloudVideoView);
//        mLivePlayer.setOnLivePlayerCallback(this);

//        initRTMPURL(params);

//        startPlay();

    }


    // 初始化完成之后自动播放
    public void startPlay() {
        mLivePlayer.startPlay();
    }

    private void initRTMPURL(Map<String, Object> params) {
        //播放类型
        int activityType = (int) params.get(Constants.INTENT_ACTIVITY_TYPE);
        if (activityType == 0) {
            //获取不到就默认是标准的
            activityType = Constants.ACTIVITY_TYPE_LIVE_PLAY;
        }
        //播放的URL
        String playURL = (String) params.get(Constants.INTENT_URL);
        if (playURL == null) {
            throw new NullPointerException("播放的地址不能为null");
        }
        mLivePlayer.setPlayURL(activityType, playURL);
    }

    @Override
    public View getView() {
        return txCloudVideoView;
    }

    @Override
    public void dispose() {
        if (mLivePlayer != null) {
            mLivePlayer.stopPlay();
            mLivePlayer.destroy();
        }
    }

    @Override
    public void onPlayStart(int code) {

    }

    @Override
    public void onPlayStop() {

    }

    @Override
    public void onPlayEvent(int event, Bundle param) {
//        Log.d(TAG, "receive event: " + event + ", " + param.getString(TXLiveConstants.EVT_DESCRIPTION));
//        mLogInfoWindow.setLogText(null, param, event);

        /**
         * 调用flutter的例子
         */
        methodChannel.invokeMethod("onPlayEvent", event);

        switch (event) {
            case TXLiveConstants.PLAY_EVT_PLAY_BEGIN:
            case TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME:
//                stopLoadingAnimation();
                break;
            case TXLiveConstants.PLAY_EVT_PLAY_LOADING:
//                startLoadingAnimation();
                break;
            case TXLiveConstants.PLAY_EVT_CHANGE_RESOLUTION:
//                Log.d(TAG, "size " + param.getInt(TXLiveConstants.EVT_PARAM1) + "x" + param.getInt(TXLiveConstants.EVT_PARAM2));
                break;
            case TXLiveConstants.PLAY_EVT_GET_MESSAGE:
                byte[] data = param.getByteArray(TXLiveConstants.EVT_GET_MSG);
                String seiMessage = "";
                if (data != null && data.length > 0) {
                    try {
                        seiMessage = new String(data, "UTF-8");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
//                Toast.makeText(getApplicationContext(), seiMessage, Toast.LENGTH_SHORT).show();
                break;
            case TXLiveConstants.PLAY_EVT_CHANGE_ROTATION:
            case TXLiveConstants.PLAY_ERR_NET_DISCONNECT:
            case TXLiveConstants.PLAY_EVT_PLAY_END:
                break;
        }
        if (event < 0) {
//            Toast.makeText(mContext, param.getString(TXLiveConstants.EVT_DESCRIPTION), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onNetStatus(Bundle bundle) {

    }

    @Override
    public void onFetchURLStart() {

    }

    @Override
    public void onFetchURLFailure() {

    }

    @Override
    public void onFetchURLSuccess(String url) {

    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        String method = call.method;
        switch (method) {
            case MethodName.START_PLAY:
                startPlay();
                result.success(null);
                break;
            case MethodName.TOGGLE_PLAY:
                mLivePlayer.togglePlay();
                result.success(null);
                break;

            case MethodName.STOP_PLAY:
                mLivePlayer.stopPlay();
                result.success(null);
                break;
            default:
                result.notImplemented();
        }


    }
}
